package com.example.androidlocation_finder;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {
	private GoogleMap map;
	boolean enabled = false;
	private Utils utils;
	boolean showCreatedDialog = false;
	private Criteria criteria;
	private Location location;
	private String provider;
	private LocationManager locationManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		utils = new Utils();

		if (!checkGPS()) {
			utils.createDialog(MainActivity.this);
		}

		if (checkGPS()) {
			createProvider();
			Toast.makeText(
					getApplicationContext(),
					String.valueOf(getLatitude()) + " "
							+ String.valueOf(getLongitude()),
					Toast.LENGTH_SHORT).show();
		}
		createMap();
		settingCurrentLocation(getLatitude(), getLongitude());

	}

	private void createProvider() {
		criteria = new Criteria();
		provider = locationManager.getBestProvider(criteria, false);
		location = locationManager.getLastKnownLocation(provider);

	}

	private double getLatitude() {
		return location.getLatitude();
	}

	private double getLongitude() {
		return location.getLongitude();
	}

	private boolean checkGPS() {
		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		enabled = locationManager
				.isProviderEnabled(LocationManager.GPS_PROVIDER);
		return enabled;
	}

	private void settingCurrentLocation(double Latitude, double Longitude) {

		/*Object for the current location*/
		LatLng currentLocation = new LatLng(Latitude, Longitude);
		/*Moving the camera to the specific location with appropriate zom*/
		map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15));
		/*Bubble bush the red marker*/
		map.addMarker(new MarkerOptions().title("Review")
				.snippet("No review")
				.position(currentLocation));

	}

	private void createMap() {
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();
		map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		map.setMyLocationEnabled(true);

	}

}
